#!/usr/bin/python

import sys
import os
import re
from ssh_shake import ssh_shake as ssh

class mount():

    __Excute = None

    def __init__(self):
        self.__Excute=os.system
        return

    def mount_disk(self,ip,dev,mountpoint,port=22):
        S = ssh()
        conn = S.create(ip,port)
        reader = os.popen("ssh "+ip+" parted -s "+dev+" mklabel msdos print")
        size = None
        data = reader.readlines()
        reader.close()
        for d in data:
            if re.match(r"Disk /dev/.*",d):
                x = re.search(r"Disk /dev/\w+: (.*)GB",d)
                if x:
                    size = round(float(x.group(1)))
                    if size > 1:
                        size = size - 1
                else:
                    x = re.search(r"Disk /dev/\w+: (.*)MB",d)
                    size = int(x.group(1))//1024
        size = str(int(size))
        command = "parted -s "+dev+" mklabel msdos; parted -s "+dev+" mkpart primary 1 "+size+"GB; mkfs.xfs -f "+dev+"1; mount "+dev+"1 "+ mountpoint +";echo "+ dev + "1 "+ mountpoint +" xfs rw 0 0  >> /etc/fstab"
        stdin,stdout,stderr = conn.exec_command(command)
        return

    
if __name__ == "__main__":
    dev = "dev/sdc"
    mount_disk("172.22.29.175",dev)
